<?php

/*
 *                                                                          
 *
 */

abstract class GZ_Api
{
    public static $pagination = array();
	public static $token = '';
	public static $access_id = '';
	public static $api_module = '';
	
    protected static $error = array(
		0	=> 'error',
		1	=> 'succeed',
		2	=> 'message',
		3 	=> 'Missing Parameters',
        6   => '用户名或密码错误',
		7 	=> '密码错误',
		11  => '您的账号未审核，暂无法登录',
		13  => '不存在的信息',
	    99	=> '您的帐号已在其他设备登陆',
        100 => '账号已过期请重新登录',
        101 => '协议格式错误',
		102 => '不存在的接口',
		103 => 'token已过期',
        501 => '协议格式错误',
        502 => '协议格式错误',
        503 => '合同期终止',
		20001  => '手机未注册',
		20003  => '手机号不能为空',
		20005  => '手机号格式不正确',
		2000  => '手机已被注册',
		2001  => '请输入手机验证码',
		2002  => '手机验证码已过期',
		2003  => '手机验证码错误',
		2004  => '每60秒内只能发送一次短信验证码，请稍候重试',
		2005  => '您发送验证码太过于频繁，请稍后重试！',
		2006  => '短信验证码发送失败'
    );

	
	public static function url()
	{
		$url =  "http://".$_SERVER['HTTP_HOST']."/";
		return $url;
	}
	
	//data\afficheimg
	public static function get_url($url)
	{
		if( strlen($url) < 2)
		{
			return Gz_Api::null_img();
		}
		else
		{
			if(preg_match('/(http:\/\/)|(https:\/\/)/i', $url)){
				return $url;	
			}else{
				$url = strpos($url,'/')==0 ? $url : '/'.$url;
				return "http://".$_SERVER['HTTP_HOST'].$url;
			}
		}
		
	}
	
	public static function null_img()
	{
		$no_url = "http://".$_SERVER['HTTP_HOST']."/Mapi/public/images/no_picture.png";
		return $no_url;
	}
	
	
    public static function init()
    {
        if (!empty($_POST['json'])) 
		{
			if (get_magic_quotes_gpc()) 
			{
				$_POST['json'] = stripslashes($_POST['json']);
			}
			
			
			$logger_json = $_POST['json'];
            $_POST = json_decode($_POST['json'], true);
			
			$tmp_arr = explode("/",$_SERVER["QUERY_STRING"]);
			self::$api_module = $tmp_arr[1].'_'.$tmp_arr[2];
			
			$logger = 'API:'.$tmp_arr[1].'/'.$tmp_arr[2].'  JSON:'.$logger_json;
			api_logger($logger);
				
        }
		
		self::$token = _POST('token');
        self::$pagination = _POST('pagination', array('page' => 1, 'count' => 10));

    }
	
	public static function get_accesskey()
    {
        self::$access_id = md5(uniqid(mt_rand(), true));

		define('ACCESS_ID', self::$access_id);
		
        return self::$access_id;
    }


	public static function authToken(){
		
		$input_token = _POST('token','');
		$user_id = _POST('user_id',0);
		
		if(empty($user_id)){
			self::outPut(3);
		}
	 	if(empty($input_token)){
            self::outPut(100);
        }
		
		$sql = 'select token,expiry from '.$GLOBALS['db']->table('users_token').
		' where user_id ='.$user_id.' LIMIT 1';
		$data = $GLOBALS['db']->getRow($sql);
		
		if($input_token!=$data['token']){
			self::outPut(100);
		}
		
	}
	
	
	public static function message($info='',$code=2){

		$status		 = self::$error[$code];
		$mess_info	 = empty($info)?'Missing Parameters':trim($info);
		$data = array('message' => array('msg_code' => $code,'msg_status' => $status,'msg_desc' => $mess_info));
		$outPut['response']= $data;
        die(json_encode($outPut));
		
	}
	
    public static function outPut($data)
    {
		$module = '';
        if (!is_array($data)) {
            $data = array(
                'message' => array(
					'msg_code' => 0,
                    'msg_status' => 'error',
                    'msg_desc' => self::$error[$data]
                )
			 );
			$outPut['response']= $data;
			die(json_encode($outPut));
        }
		
		if(!empty(self::$api_module)){
			$module = self::$api_module;
		}else{
			$module = 'res_data';
		}
		
		$msg_data = array('message' => array('msg_code' => 1,'msg_status' => 'succeed','msg_desc' => 'api:'.$module));
		$api_data = array_merge(array('data'=>$data), $msg_data);
		$response['response'] = $api_data;
        die(json_encode($response));
    }
		
	
}